package br.ufrj.ppgi.infovis.util;

import org.w3c.dom.*;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.xml.sax.SAXException;

import br.ufrj.ppgi.infovis.mtgraph.Graph;
import br.ufrj.ppgi.infovis.mtgraph.GraphEdgeManager;
import br.ufrj.ppgi.infovis.mtgraph.GraphNode;
import br.ufrj.ppgi.infovis.mtgraph.GraphScene;
import br.ufrj.ppgi.infovis.styles.Category;
import br.ufrj.ppgi.infovis.styles.Configuration;

public class XMLConfigImport { 

	private String pathName;
	
	public XMLConfigImport(String path){		
		this.pathName = path;
	}
	
	//importa vertices
	public Configuration Import() {
		
		Configuration config = null;
		

		try {
			DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
			Document doc = docBuilder.parse(this.pathName);

			Element elem = doc.getDocumentElement();
			NodeList nl= elem.getElementsByTagName("title");
			config = new Configuration(nl.item(0).getTextContent());
			
			nl= elem.getElementsByTagName("category");
						
			int l = nl.getLength();

			for( int i=0; i<l; i++ ) {  
				
				
				Element tagCategory = (Element) nl.item( i );  
				
				NodeList tagName = tagCategory.getElementsByTagName("name");
				String name = tagName.item(0).getTextContent();
				
				NodeList tagColor = tagCategory.getElementsByTagName("color");
				String color = tagColor.item(0).getTextContent();
				
				NodeList tagIcon = tagCategory.getElementsByTagName("icon");
				String icon = tagIcon.item(0).getTextContent();
				
				
				config.addCategory(new Category(name, color, icon));				
			}

		} catch (SAXException e) {
			Exception x = e.getException ();
			((x == null) ? e : x).printStackTrace ();

		} catch (Throwable t) {
			t.printStackTrace ();
		}
		return config;
	}
	

}
